﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using BenefitHelper.Sys;
namespace BenefitHelper.Data.Year
{
    public class AccountYearMoney
    {
        /// <summary>
        /// 编号
        /// </summary>
        public int Id { get; set; }
        /// <summary>
        /// 账户
        /// </summary>
        public int AccountId { get; set; }
        /// <summary>
        /// 账户
        /// </summary>
        public virtual Account Account { get; set; }
        /// <summary>
        /// 多仓持仓
        /// </summary>
        public int LongPosition { get; set; }
        /// <summary>
        /// 空仓持仓
        /// </summary>
        public int ShortPosition { get; set; }
        /// <summary>
        /// 期初结存
        /// </summary>
        public double PresetTlePosit { get; set; }
        /// <summary>
        /// 期末结存
        /// </summary>
        public double SettlePosit { get; set; }
        /// <summary>
        /// 冻结保证金
        /// </summary>
        public double FrozenMargin { get; set; }
        /// <summary>
        /// 冻结手续费
        /// </summary>
        public double FrozenCommission { get; set; }
        /// <summary>
        /// 入金金额
        /// </summary>
        public double Deposit { get; set; }
        /// <summary>
        /// 出金金额
        /// </summary>
        public double WithDraw { get; set; }
        /// <summary>
        /// 交割保证金
        /// </summary>
        public double DeliveryMargin { get; set; }
        /// <summary>
        /// 交割手续费
        /// </summary>
        public double DeliveryCommission { get; set; }
        /// <summary>
        /// 追加保证金
        /// </summary>
        public double AppendMargin { get; set; }
        /// <summary>
        /// 基础保证金
        /// </summary>
        public double BaseMargin { get; set; }
        /// <summary>
        /// 质押金额
        /// </summary>
        public double Mortgage { get; set; }
        /// <summary>
        /// 上次质押金额
        /// </summary>
        public double Premortgage { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Commission { get; set; }
        /// <summary>
        /// 可用资金
        /// </summary>
        public double AvailableMoney { get; set; }
        /// <summary>
        /// 可取资金
        /// </summary>
        public double WithDrawQuota { get; set; }
        /// <summary>
        /// 动态权益
        /// </summary>
        public double DynamicMoney { get; set; }
        /// <summary>
        /// 占用保证金
        /// </summary>
        public double Margin { get; set; }
        /// <summary>
        /// 平仓盈亏
        /// </summary>
        public double CloseProfit { get; set; }
        /// <summary>
        /// 持仓盈亏
        /// </summary>
        public double PositionProfit { get; set; }
        /// <summary>
        /// 风险度
        /// </summary>
        public double RiskRate { get; set; }
        /// <summary>
        /// 交易年
        /// </summary>
        public int Year { get; set; }





        /// <summary>
        /// 删除当年的记录
        /// </summary>
        /// <param name="month"></param>
        /// <param name="year"></param>
        public void Delete(int year, DB.DBManager db)
        {
            var query = from t in db.AccountYearMoney where t.Year == year select t;
            foreach (Data.Year.AccountYearMoney c in query)
            {
                db.AccountYearMoney.Remove(c);
            }
            db.SaveChanges();
        }

        /// <summary>
        /// 初始化账户年资金信息
        /// </summary>
        /// <param name="month"></param>
        /// <param name="year"></param>
        public void InitAccountYearMoney(int year, DB.DBManager db)
        {
            var query = db.Accounts.ToList();
            foreach (Sys.Account account in query)
            {
                AccountYearMoney change = new AccountYearMoney();
                Data.Month.AccountMonthMoney lastMonthMoney = new Data.Month.AccountMonthMoney();
                Data.Month.AccountMonthMoney fristMonthMoney = new Data.Month.AccountMonthMoney();
                change.AccountId = account.Id;
                var _query = (from t in db.AccountMonthMoney where t.AccountId == account.Id && t.Year == year orderby t.Id descending select t).ToList();
                if (_query.Count() > 0)
                {
                    lastMonthMoney = _query.First();
                    fristMonthMoney = _query.LastOrDefault();
                }
                var existQuery = from t in db.AccountYearMoney where t.Year == year && t.AccountId == account.Id select t;
                if (existQuery.Count() > 0)
                {
                    change = existQuery.First();
                }

                change.AppendMargin = lastMonthMoney.AppendMargin;
                change.AvailableMoney = lastMonthMoney.AvailableMoney;
                change.BaseMargin = lastMonthMoney.BaseMargin;
                change.CloseProfit = _query.Sum(a => a.CloseProfit);
                change.Commission = _query.Sum(a => a.Commission);
                change.DeliveryCommission = _query.Sum(a => a.DeliveryCommission);
                change.DeliveryMargin = lastMonthMoney.DeliveryMargin;
                change.Deposit = _query.Sum(a => a.Deposit);
                change.DynamicMoney = lastMonthMoney.DynamicMoney;
                change.FrozenCommission = lastMonthMoney.FrozenCommission;
                change.FrozenMargin = lastMonthMoney.FrozenMargin;
                change.LongPosition = lastMonthMoney.LongPosition;
                change.Margin = lastMonthMoney.Margin;
                change.Mortgage = lastMonthMoney.Mortgage;
                change.PositionProfit = lastMonthMoney.PositionProfit;
                change.Premortgage = fristMonthMoney.Premortgage;
                change.PresetTlePosit = fristMonthMoney.PresetTlePosit;
                change.RiskRate = lastMonthMoney.RiskRate;
                change.SettlePosit = lastMonthMoney.SettlePosit;
                change.ShortPosition = lastMonthMoney.ShortPosition;
                change.WithDraw = _query.Sum(a => a.WithDraw);
                change.WithDrawQuota = lastMonthMoney.WithDrawQuota;
                change.Year = year;
                if (_query.Count() == 0)
                {
                    db.AccountYearMoney.Add(change);
                }
            }
            db.SaveChanges();
        }


        /// <summary>
        /// 获取账号和交易员年资金信息
        /// </summary>
        /// <param name="operatorid"></param>
        /// <param name="month"></param>
        /// <param name="year"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public List<AccountYearMoney> GetAccountAndAllChange(int operatorid, int year, DB.DBManager db)
        {
            List<AccountYearMoney> list = new List<AccountYearMoney>();
            Sys.TradeHistory th = new Sys.TradeHistory();
            th = th.GetYearLastTradingDay(year, db);
            List<Sys.Account> accounts = new Sys.DayOperatorAccount().GetDayOperatorAccounts(th.GetDateTimeFromTradeHistoryId(th.Id, db), operatorid, db);
            foreach (Sys.Account account in accounts)
            {
                var query = db.AccountYearMoney.Where(a => a.Year == year).Where(a => a.AccountId == account.Id);
                if (query.Count() > 0)
                {
                    list.Add(query.First());
                }
            }
            return list;
        }
    }
}